package com.dlmz.view;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.dlmz.model.HealthAmt;
import com.dlmz.util.BaseDao;
import com.dlmz.util.ResultsetToHealthAmt;

public class HealthChangeModel extends AbstractTableModel{

	@SuppressWarnings("rawtypes")
	private Vector columnNames;  
    @SuppressWarnings("rawtypes")
	private Vector rowDates;  
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
   	public void queryHealthIncrease(String sql, String startYear, String endYear)  
   {  
    	BaseDao baseDao = null;  
        //========初始化JTable信息  
        columnNames = new Vector();  
        rowDates = new Vector();   
        columnNames.add("年度范围"); 
        columnNames.add("健康人数年增长"); columnNames.add("健康人数年增长率(%)");  
        
        baseDao = new BaseDao();  
       //获取开始年度的健康人数
        List list1= baseDao.query(sql, new ResultsetToHealthAmt(),startYear); 
      //获取结束年度的健康人数
        List list2= baseDao.query(sql, new ResultsetToHealthAmt(),endYear); 
        
        if (list1.size()>0 && list2.size()>0) {
        	
        	HealthAmt healthAmtStart=(HealthAmt) list1.get(0);
        	HealthAmt healthAmtEnd=(HealthAmt) list2.get(0);

			Vector row = new Vector(); ; 
			row.add(startYear + "~"+ endYear);
			// 健康人数年增长
            row.add(healthAmtEnd.getHealthAmt()-healthAmtStart.getHealthAmt()); 
         // 健康人数年增长率
            double increaseRate = (healthAmtEnd.getHealthAmt()-healthAmtStart.getHealthAmt())*100/healthAmtStart.getHealthAmt();
            row.add(increaseRate);
            
            rowDates.add(row);
        }

   }
    
    public double queryAvgHealthIncr(String sql) {
    	// 无统计数据时
    	double avgInrRate = -999;
    	BaseDao baseDao = null;  
        //========初始化JTable信息  
        columnNames = new Vector();  
        rowDates = new Vector();   
        columnNames.add("年度"); 
        columnNames.add("健康人数"); 
        
        baseDao = new BaseDao();  
        List list= baseDao.query(sql, new ResultsetToHealthAmt()); 
        
        if (list.size()> 1) {
        	//获取统计首年的健康人数
        	HealthAmt healthAmtStart=(HealthAmt) list.get(0);
        	//获取统计末年的健康人数
        	HealthAmt healthAmtEnd=(HealthAmt) list.get(list.size()-1);
        	
        	// 经过年数N
        	int n = healthAmtEnd.getExmYear() - healthAmtStart.getExmYear();
        	// 末年除以首年的健康人数开n次根-1
        	double m = StrictMath.pow(healthAmtEnd.getHealthAmt()/healthAmtStart.getHealthAmt(), 1.0/n) - 1;
        	// 年平均增长百分比
        	avgInrRate = m*100;
        } else if (list.size()== 1) {
        	// 首年末年相同时
        	avgInrRate = 0;
        } 
        
        
        return avgInrRate;
	}
    
    public int getColumnCount() {  
    	  //告知列数，用标题数组的大小,这样表格就是rowData.size()行，columnNames.size()列了  
    	    return this.columnNames.size();  
    	 }  
    	  
	    public int getRowCount() {   
	        return this.rowDates.size(); 
	        //这里是告知表格应该有多少行，我们返回rowDatas上的String数组个数
	    }  
	  
	    @SuppressWarnings("rawtypes")
		public Object getValueAt(int row, int col) {  
	        // TODO Auto-generated method stub  
	        if(!rowDates.isEmpty())  
	            return ((Vector)this.rowDates.get(row)).get(col);  
	        else  
	            return null;  
	    }  
	    @Override  
	    public String getColumnName(int column) {  
	        return (String)this.columnNames.get(column);  
	      //这里是告知表格各列的列名，我们返回该列的列名
	    }    

}
